home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 19D (1987-07-22)(Pacific North-West Amigas Club)[WB].zip / Panorama - Disk 19D (1987-07-22)(Pacific North-West Amigas Club)[WB].adf / PipeHandler1.2 / pipedebug.c < prev    next >
C/C++ Source or Header  |  1987-06-28  |  7KB  |  296 lines

  1. /****************************************************************************
  2. **  File:       pipedebug.c
  3. **  Program:    pipe-handler - an AmigaDOS handler for named pipes
  4. **  Version:    1.1
  5. **  Author:     Ed Puckett      qix@mit-oz
  6. **
  7. **  Copyright 1987 by EpAc Software.  All Rights Reserved.
  8. **
  9. **  History:    05-Jan-87       Original Version (1.0)
  10. */
  11.  
  12. #include   <libraries/dos.h>
  13. #include   <libraries/dosextens.h>
  14. #include   <exec/exec.h>
  15.  
  16. #include   "pipedebug.h"
  17.  
  18.  
  19.  
  20. /*---------------------------------------------------------------------------
  21. ** pipedebug.c
  22. ** -----------
  23. ** This module contains debugging functions.  In need only be included if the
  24. ** other modules are compiled with DEBUG defined.
  25. **
  26. ** Visible Functions
  27. ** -----------------
  28. **    int   InitDebugIO    (NodePri)
  29. **    void  CleanupDebugIO ()
  30. **    BPTR  DebugOpen      (name, mode)
  31. **    void  DebugClose     (fh)
  32. **    int   DebugWrite     (fh, buf, len)
  33. **    void  OutStr         (str, fh)
  34. **    void  OutLONG        (n, fh)
  35. **
  36. ** Macros (in pipedebug.h)
  37. ** -----------------------
  38. **    OS (s)
  39. **    NL
  40. **    OL (n)
  41. **
  42. ** Local Functions
  43. ** ---------------
  44. **    void  DebugIO (Handler, Type, Arg1, Arg2, Arg3)
  45. */
  46.  
  47.  
  48.  
  49. #define   BPTRtoCptr(Bp)   ((char *) ((ULONG) (Bp) << 2))
  50. #define   CptrtoBPTR(Cp)   ((BPTR)   ((ULONG) (Cp) >> 2))
  51.  
  52.  
  53.  
  54. #define   MEMFLAGS   (MEMF_PUBLIC | MEMF_CLEAR)
  55.  
  56.  
  57.  
  58. static struct MsgPort    *DebugDOSPort  =  NULL;
  59. static struct Message    *DebugMsg      =  NULL;
  60. static struct DosPacket  *DebugPkt      =  NULL;
  61. BPTR                      DebugFH       =  0;
  62.  
  63.  
  64.  
  65. /*---------------------------------------------------------------------------
  66. ** InitDebugIO() allocates things for the debugging functions, and opens a
  67. ** window for output.  It MUST be called before any of the I/O operations
  68. ** are used.  CleanupDebugIO() frees the resources allocated here, and closes
  69. ** the window.
  70. **      The routines DebugOpen(), DebugClose(), and DebugWrite() mimic their
  71. ** corresponding DOS functions, except they use a private reply port, not
  72. ** the process' DOS port.  DOS does bad things if a handler request comes in
  73. ** while it is waiting or a reply to one of its requests made on your behalf.
  74. **      The return value is nonzero iff no error occurred.
  75. */
  76.  
  77. int  InitDebugIO (NodePri)
  78.  
  79. BYTE  NodePri;
  80.  
  81. { struct MsgPort  *CreatePort();
  82.   BYTE            *AllocMem();
  83.  
  84.  
  85.   DebugDOSPort=  NULL;
  86.   DebugMsg=      NULL;
  87.   DebugPkt=      NULL;
  88.   DebugFH=       0;
  89.  
  90.   if ( ((DebugDOSPort= CreatePort (NULL, NodePri)) == NULL)  ||
  91.        ((DebugMsg= (struct Message   *) AllocMem (sizeof (struct Message),   MEMFLAGS)) == NULL) ||
  92.        ((DebugPkt= (struct DosPacket *) AllocMem (sizeof (struct DosPacket), MEMFLAGS)) == NULL) ||
  93.        ((DebugFH= DebugOpen (DEBUG_CON_NAME, MODE_NEWFILE)) == 0) )
  94.     { CleanupDebugIO ();
  95.       return FALSE;
  96.     }
  97.  
  98.   return TRUE;
  99. }
  100.  
  101.  
  102.  
  103. /*---------------------------------------------------------------------------
  104. ** Cleanup things allocated by InitDebugIO, and close the window.    
  105. */
  106.  
  107. void  CleanupDebugIO ()
  108.  
  109. { void  FreeMem();
  110.  
  111.  
  112.   if (DebugFH != 0)
  113.     DebugClose (DebugFH);
  114.  
  115.   if (DebugPkt != NULL)
  116.     FreeMem (DebugPkt, sizeof (struct DosPacket));
  117.  
  118.   if (DebugMsg != NULL)
  119.     FreeMem (DebugMsg, sizeof (struct Message));
  120.  
  121.   if (DebugDOSPort != NULL)
  122.     { FreeSignal (DebugDOSPort->mp_SigBit);
  123.       FreeMem (DebugDOSPort, sizeof (struct MsgPort));
  124.     }
  125. }
  126.  
  127.  
  128.  
  129. /*---------------------------------------------------------------------------
  130. ** DebugOpen() performs just like the DOS function Open().
  131. ** InitDebugIO() MUST have been called and returned successful before calling
  132. ** this function.
  133. */
  134.  
  135. BPTR  DebugOpen (name, mode)
  136.  
  137. char  *name;
  138. int   mode;
  139.  
  140. { char               Bnamebuf[DEBUGOPEN_MAXNAMELEN + 3 + 2], *Bname;
  141.   UBYTE              namelen;
  142.   struct MsgPort     *HandlerPID, *DeviceProc();
  143.   int                IoErr();
  144.   struct FileLock    *Lock;
  145.   struct FileHandle  *handle;
  146.   BYTE               *AllocMem();
  147.   void               DebugIO(), FreeMem();
  148.  
  149.  
  150.   Bname= (char *) (((ULONG) Bnamebuf + 3) & (~0 << 2));     /* longword align */
  151.  
  152.   for (namelen= 0; (Bname[namelen + 1]= name[namelen]); ++namelen)
  153.     if (namelen > DEBUGOPEN_MAXNAMELEN)
  154.       return 0;
  155.  
  156.   Bname[0]= (char) namelen;     /* make a BSTR */
  157.  
  158.  
  159.   HandlerPID= DeviceProc (name);
  160.   if (HandlerPID == NULL)
  161.     return 0;
  162.  
  163.   Lock= (struct FileLock *) IoErr ();
  164.  
  165.  
  166.   if ((handle= (struct FileHandle *) AllocMem (sizeof (struct FileHandle), MEMFLAGS)) == NULL)
  167.     return 0;
  168.  
  169.   handle->fh_Pos= -1;
  170.   handle->fh_End= -1;
  171.   handle->fh_Type= HandlerPID;
  172.  
  173.  
  174.   DebugIO (HandlerPID, mode, CptrtoBPTR (handle), CptrtoBPTR (Lock), CptrtoBPTR (Bname));
  175.  
  176.   if (DebugPkt->dp_Res1 == 0)
  177.     { FreeMem (handle, sizeof (struct FileHandle));
  178.       return 0;
  179.     }
  180.  
  181.   return  CptrtoBPTR (handle);
  182. }
  183.  
  184.  
  185.  
  186. /*---------------------------------------------------------------------------
  187. ** DebugClose() performs just like the DOS function Close().
  188. ** InitDebugIO() MUST have been called and returned successful before calling
  189. ** this function.
  190. */
  191.  
  192. void  DebugClose (fh)
  193.  
  194. BPTR  fh;
  195.  
  196. { struct FileHandle  *handle;
  197.   void               DebugIO(), FreeMem();
  198.  
  199.   handle= (struct FileHandle *) BPTRtoCptr (fh);
  200.   DebugIO (handle->fh_Type, 1007, handle->fh_Arg1, 0, 0);
  201.   FreeMem (handle, sizeof (struct FileHandle));
  202. }
  203.  
  204.  
  205.  
  206. /*---------------------------------------------------------------------------
  207. ** DebugWrite() performs just like the DOS function Write().
  208. ** InitDebugIO() MUST have been called and returned successful before calling
  209. ** this function.
  210. */
  211.  
  212. int  DebugWrite (fh, buf, len)
  213.  
  214. BPTR   fh;
  215. BYTE   *buf;
  216. ULONG  len;
  217.  
  218. { struct FileHandle  *handle;
  219.   void               DebugIO();
  220.  
  221.   handle= (struct FileHandle *) BPTRtoCptr (fh);
  222.   DebugIO (handle->fh_Type, ACTION_WRITE, handle->fh_Arg1, buf, len);
  223.   return DebugPkt->dp_Res1;
  224. }
  225.  
  226.  
  227.  
  228. /*---------------------------------------------------------------------------
  229. ** DebugIO() sets up the DosPacket with the specified information, initiates
  230. ** the request, and waits for the reply.
  231. */
  232.  
  233. static void  DebugIO (Handler, Type, Arg1, Arg2, Arg3)
  234.  
  235. struct MsgPort  *Handler;
  236. LONG            Type;
  237. LONG            Arg1;
  238. LONG            Arg2;
  239. LONG            Arg3;
  240.  
  241. { void            PutMsg();
  242.   struct MsgPort  *WaitPort(), *Getmsg();
  243.  
  244.  
  245.   DebugMsg->mn_ReplyPort=    DebugDOSPort;
  246.   DebugMsg->mn_Node.ln_Type= NT_MESSAGE;
  247.   DebugMsg->mn_Node.ln_Name= (char *) DebugPkt;
  248.  
  249.   DebugPkt->dp_Link= DebugMsg;
  250.   DebugPkt->dp_Port= DebugDOSPort;
  251.   DebugPkt->dp_Type= Type;
  252.   DebugPkt->dp_Arg1= Arg1;
  253.   DebugPkt->dp_Arg2= Arg2;
  254.   DebugPkt->dp_Arg3= Arg3;
  255.  
  256.   PutMsg (Handler, DebugMsg);
  257.   (void) WaitPort (DebugDOSPort);
  258.   (void) GetMsg (DebugDOSPort);     /* assume it is DebugMsg */
  259. }
  260.  
  261.  
  262.  
  263. /*---------------------------------------------------------------------------
  264. ** OutStr() outputs the null-terminated string "str" to the filehandle "fh".
  265. */
  266.  
  267. void  OutStr (str, fh)
  268.  
  269. char  *str;
  270. BPTR  fh;
  271.  
  272. { int  strlen();
  273.  
  274.   DebugWrite (fh, str, strlen (str));
  275. }
  276.  
  277.  
  278.  
  279. /*---------------------------------------------------------------------------
  280. ** OutLONG() outputs the decimal representaion on "n" to the filehandle "fh".
  281. ** The conversion function stcu_d() is used -- this may not be available
  282. ** on all systems.  In that case, such a function will need to be written.
  283. */
  284.  
  285. void  OutLONG (n, fh)
  286.  
  287. ULONG  n;
  288. BPTR   fh;
  289.  
  290. { char  buf[80];
  291.   int   stcu_d();     /* Lattice C Library conversion function */
  292.  
  293.   (void) stcu_d (buf, n, 80);
  294.   OutStr (buf, fh);
  295. }
  296.